package com.pos.clothes.service.serviceImpl;

import com.pos.clothes.mapper.OrderMapper;
import com.pos.clothes.pojo.Discount;
import com.pos.clothes.pojo.Norms;
import com.pos.clothes.pojo.Order;
import com.pos.clothes.pojo.Order_detail;
import com.pos.clothes.service.DiscountService;
import com.pos.clothes.service.NormsService;
import com.pos.clothes.service.OrderService;
import com.pos.clothes.service.Strategy;
import com.pos.clothes.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class    OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper mapper;

    @Autowired
    NormsService normsService;
    @Autowired
    public DiscountService discountService;
    @Qualifier("discountStrategy")
    @Autowired
    Strategy strategy1;
    @Qualifier("declineStrategy")
    @Autowired
    Strategy strategy2;
    @Override
    public List<Norms> searchNormByCode(Norms norms) {
        return mapper.searchNormByCode(norms);
    }
    Norms norms = new Norms();
    @Override
    public Integer updateNorm(List<Norms> normsList) {
        try {
            for (int i=0;i< normsList.size();i++){
                norms = normsList.get(i);
                normsService.count_updateNorms(norms);
            }
        }catch (Exception e){
            return -1;
        }
        return 1;
    }

    @Override
    public Map<String, Float> calculation(List list, Float pay, int iddiscount) {
        Discount discount;//统一折扣
        List<Discount> listdiscount;//满额折扣
        List<Integer> idList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();
        //解析获取id列表和数量列表
        for (int i=0;i<list.size();i++){
            Map<String,Integer> normObject = (Map<String, Integer>) list.get(i);
            idList.add(normObject.get("id"));
            numList.add(normObject.get("num"));
        }
        List<Integer> priceList = normsService.count_searcNorm(idList);
        //计算总价格
        float sumPrice = 0;//总价格
        float needPay = 0;//打折后价格
        float returnPay = 0;//找零价格
        for (int i = 0;i<list.size();i++){
            sumPrice+=priceList.get(i)*numList.get(i);
        }
        //获取打折
//        if(iddiscount==1){
//            discount = discountService.getAllDiscount();
//            needPay = sumPrice*discount.getAll_rule();
//            returnPay = pay-needPay;
//        }else{
//            listdiscount = discountService.getBorderDiscount();
//            if (sumPrice<listdiscount.get(0).getBorder()){
//                needPay = sumPrice;
//                returnPay = pay-needPay;
//            }else if(sumPrice>listdiscount.get(0).getBorder()&&sumPrice<listdiscount.get(1).getBorder()){
//                needPay = sumPrice-listdiscount.get(0).getBorder_rule();
//                returnPay = pay-needPay;
//            }else if(sumPrice>listdiscount.get(1).getBorder()&&sumPrice<listdiscount.get(2).getBorder()){
//                needPay = sumPrice-listdiscount.get(1).getBorder_rule();
//                returnPay = pay-needPay;
//            }else{
//                needPay = sumPrice-listdiscount.get(2).getBorder_rule();
//                returnPay = pay-needPay;
//            }
//        }
        if(iddiscount==1){

            needPay =strategy1.discount(sumPrice);
            returnPay = pay-needPay;
        }
        if(iddiscount == 2){

            needPay = strategy2.discount(sumPrice);
            returnPay = pay-needPay;
        }

        //构造返回值
        Map<String,Float> returnMap = new HashMap<>();
        if (returnPay<0){
            return null;
        }
        returnMap.put("total",needPay);
        returnMap.put("pay",pay);
        returnMap.put("reurnPay",returnPay);
        returnMap.put("sumPrice",sumPrice);
        return returnMap;
    }

    @Override
    public int insertOrder(Order order) {
        mapper.addOrder(order);
        return order.getIdorder();
    }

    @Override
    public Integer insertOrderDetails(List<Order_detail> orderDetailsList) {
        for (int i=0;i<orderDetailsList.size();i++){
            mapper.addOrder_detail(orderDetailsList.get(i));
        }
        return null;
    }

    @Override
    public List<Order> getAllOrder() {
        return mapper.getAllOrder();
    }

    @Override
    public List<Order_detail> getOrderDetail(int idorder) {
//        List<Map<String,Object>> list = mapper.getOrderDetail(idorder);
//        for (int i =0;i<list.size();i++){
//            Map<String,Object> element = (Map<String,Object>) list.get(i);
//            Object n = element.get("number");
//            Map<String,Object> element1 = element;
//            Object j = element1.get("norms");
//            System.out.println((n*j);
//            Map<String,Object> element = list.get(i);
//            Integer n = (Integer)element.get("number");
//            System.out.println(n);
//        }
        return mapper.getOrderDetail(idorder);
    }
//
//    @Override
//    public Object finish(Map<String, Object> map) {
//        List<Object> list = (List<Object>) map.get("order_list");
//        int iduser = (int)map.get("iduser");
//        float pay = (int) map.get("pay");
//        int idkind = (int) map.get("idkind");
//        int iddiscount = (int)map.get("iddiscount");
//        Map<String,Float> returnMap;
//        List<Norms> normsList = new ArrayList<>();
//        for (int i =0;i<list.size();i++){
//            Map<String,Integer> normObject = (Map<String, Integer>) list.get(i);
//            Norms norms = new Norms();
//            norms.setId(normObject.get("id"));
//            norms.setRemains(normsService.count_getNormsRemain(norms)-normObject.get("num"));
//            normsList.add(norms);
//        }
//        //更新库存
//        int res = this.updateNorm(normsList);
//        if (res!=1) {
//            return JsonData.buildSuccess(res, "下单失败");
//        }
//        //更新order表
//        returnMap = this.calculation(list,pay,iddiscount);
//        Order order = new Order();
//        order.setIddiscount(iddiscount);
//        order.setIdkind(idkind);
//        order.setIduser(iduser);
//        order.setOriginalPrice(returnMap.get("sumPrice"));
//        order.setTotal(returnMap.get("total"));
//        order.setRepay(returnMap.get("reurnPay"));
//        order.setPay(pay);
//        order.setTime(new Date(System.currentTimeMillis()));
//        order.setDeal("");
//        order.setCount(normsList.size());
//        res = this.insertOrder(order);
//        //更新orderdetail表(构造列表)
//        List<Order_detail> order_details = new ArrayList<>();
//        for (int i=0;i<list.size();i++){
//            Order_detail order_detail = new Order_detail();
//            order_detail.setIdorder(res);
//            Map<String,Integer> normObject = (Map<String, Integer>) list.get(i);
//            order_detail.setIdnorm(normObject.get("id"));
//            order_detail.setNumber(normObject.get("num"));
//            order_detail.setNote("");
//            order_detail.setKey(0);
//            order_detail.setKey_discount(0);
//            order_details.add(order_detail);
//        }
//        this.insertOrderDetails(order_details);
//        return JsonData.buildSuccess();
//    }

}
